package com.suse.service.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.common.base.model.param.BasePageQueryParam;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.common.util.core.StringUtil;
import com.suse.common.util.jwt.JwtUtil;
import com.suse.model.dto.UserDto;
import com.suse.service.user.entity.User;
import com.suse.service.user.mapper.UserMapper;
import com.suse.service.user.model.param.LoginParam;
import com.suse.service.user.model.param.RegisterParam;
import com.suse.service.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.suse.common.config.response.SimpleCode.LOGIN_SUCCESS;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public R page(BasePageQueryParam pageQueryParam) {
        Long current = pageQueryParam.getCurrent();
        Long size = pageQueryParam.getSize();
        String name = pageQueryParam.getName();
        Page<User> page = new Page<>(current,size);
        page = lambdaQuery().like(StringUtil.isNotEmpty(name),User::getName,name).page(page);
        return R.put("list",page.getRecords()).put("total",page.getTotal()).success();
    }

    @Override
    public R login(LoginParam loginParam) {
        // 获取登录参数
        String phone = loginParam.getPhone();
        String password = loginParam.getPassword();
        User user = lambdaQuery().eq(User::getPhone, phone).one();
        // 判断手机号是否存在
        if (ObjectUtil.isNull(user)) {
            throw new ApiException("手机号或者密码错误！");
        }
        // 判断密码是否正确
        if (StringUtil.isNotEquals(password,user.getPassword())) {
            throw new ApiException("手机号或者密码错误！");
        }
        // 判断用户是否被禁用
        if (user.getDisabled()) {
            throw new ApiException("用户已被禁用，请联系管理员！");
        }
        // 创建 token
        String token = JwtUtil.createToken(user);
        // 返回 token 给前端
        return R.put("token",token).success(LOGIN_SUCCESS);
    }

    @Override
    public R register(RegisterParam registerParam) {
        // 获取注册参数
        String phone = registerParam.getPhone();
        // TODO 判断验证码是否正确
        // String code = userRegisterParam.getCode();
        // 判断手机号是否已被注册
        Integer count = lambdaQuery().eq(User::getPhone, phone).count();
        if (count > 1) {
            throw new ApiException("手机号已被注册");
        }
        User user = BeanUtil.copyProperties(registerParam, User.class);
        if (save(user)) {
            return R.success("注册成功");
        }else {
            throw new ApiException("注册失败！");
        }
    }

    @Override
    public UserDto getUserDtoById(String id) {
        return userMapper.getUserDtoById(id);
    }

    @Override
    public User getUserByOpenid(String openid) {
        return lambdaQuery().eq(User::getOpenid,openid).one();
    }

    @Override
    public R getDailyRegisterCount(String date) {
        int registerCount = userMapper.getDailyRegisterCount(date);
        return R.put("registerCount",registerCount).success();
    }

}
